package cn.kxm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.pojo.Model;
import cn.kxm.pojo.PartType;
import cn.kxm.pojo.Role;
import cn.kxm.service.PowerService;
@RequestMapping("/power")
@Controller
public class PowerController {

	@Autowired
	private PowerService ps;
	@RequestMapping("show")
	@ResponseBody
	public Object show(Integer rid){
		System.out.println(rid);
		List<Map<String, Object>> map=ps.findone();
		List<Map<String, Object>> lmtwo=ps.findtwo((Integer)map.get(0).get("id"));
		for(Map<String, Object> m:lmtwo){
			List<Map<String, Object>> l=ps.findthree((Integer)m.get("id"),rid);
			m.put("child", l);
		}
		map.get(0).put("child", lmtwo);
		System.out.println(JSON.toJSONString(map));
		return JSON.toJSONString(map);
	}
	@RequestMapping("showrole")
	@ResponseBody
	public Object showrole(
			@RequestParam(value="rid",required=false)Integer id,
			@RequestParam(value="roleName",required=false)String name,
			Integer page,Integer rows){
		System.out.println(id);
		List<Map<String, Object>> lm=ps.findrole(id, name, (page-1)*rows, rows);
		int total=ps.getCount(id, name);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("rows", lm);
		map.put("total", total);
		return JSONArray.toJSONString(map);
	}
	@RequestMapping("getid")
	@ResponseBody
	public Object getid(){
		int x=ps.getid();
		x=x+1;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("result", x);
		return JSON.toJSONString(map);
	}
	//查重
		@RequestMapping("check")
		@ResponseBody
		public Object check(String name){
			Map<String, Object> map=new HashMap<String, Object>();
			List<Role> l=ps.getlist();
			for (Role r : l) {
				if(r.getRoleName().equals(name)){
					map.put("result", "error");
					return JSON.toJSONString(map);
				}
			}
			map.put("result", "success");
			return JSON.toJSONString(map);
		}
	@RequestMapping("add")
	@ResponseBody
	public Object add(Role r) {
		Map<String, Object> map=new HashMap<String, Object>();
		if(ps.add(r)>0){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	
	@RequestMapping("findone")
	public String findone(String id,org.springframework.ui.Model model){
		Integer _id=Integer.parseInt(id);
		Map<String, Object> map=ps.getoneById(_id);
		model.addAttribute("role",map);
		return "web/goods/showrole";
	}
	@RequestMapping("modifyone")
	public String modifyone(String id,org.springframework.ui.Model model){
		Integer _id=Integer.parseInt(id);
		Map<String, Object> map=ps.getoneById(_id);
		model.addAttribute("role",map);
		return "web/goods/modifyrole";
	}
	@RequestMapping("modify")
	@ResponseBody
	public Object modify(Role r){
		Map<String, Object> map=new HashMap<String, Object>();
		if(ps.modifyRole(r)>0){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(Integer id){
		Map<String, Object> map=new HashMap<String, Object>();
		if(ps.delete(id)>0){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	@RequestMapping("turn")
	public String turn(Integer id,org.springframework.ui.Model model){
		model.addAttribute("rid", id);
		return "web/goods/Power";
	}
	@RequestMapping("update")
	@ResponseBody
	public Object update(Integer rid,String a){
		boolean x=true;
		Map<String, Object> map=new HashMap<String, Object>();
		ps.deleteAcl(rid);
		List<Integer> list=(List<Integer>) JSON.parse(a);
		for (Integer integer : list) {
			if(ps.insert(rid, integer)>0){
				x=true;
			}else{
				x=false;
			}
		}
		if(x){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
}
